using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreManager : MonoBehaviour
{
    public static ScoreManager Instance{ get; private set; }
    
    //分数预制体
    public GameObject scorePrefab;
    //图片数组
    public Sprite[] score3000;
    public Sprite[] score5000;
    public Sprite[] score10000;
    
    //定义字典存放分数和图片
    public Dictionary<int, Sprite[]> scoreDict;
    //初始化字典

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {
        scoreDict = new Dictionary<int, Sprite[]>();
        scoreDict.Add(3000, score3000);
        scoreDict.Add(5000, score5000);
        scoreDict.Add(10000, score10000);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ShowScore(Vector3 position, int score)  
    {
        GameObject scoreObj = Instantiate(scorePrefab, position, Quaternion.identity);

        Sprite[] scoreArray;
        scoreDict.TryGetValue(score, out scoreArray);//获取到对应的图片数组

        int index = Random.Range(0, scoreArray.Length);
        scoreObj.GetComponent<SpriteRenderer>().sprite = scoreArray[index];
        
        Destroy(scoreObj, 1.5f);//销毁对象
    }
}
